package com.sunline.poc.oozie.client.impl;

import com.sunline.poc.oozie.Application;
import com.sunline.poc.oozie.ApplicationProperties;
import com.sunline.poc.oozie.client.OozieClient;
import com.sunline.poc.oozie.config.ApplicationException;
import org.apache.oozie.client.AuthOozieClient;
import org.apache.oozie.client.OozieClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
public class OozieClientImpl implements OozieClient {


    @Autowired
    private ApplicationProperties applicationProperties;

    @Autowired
    private final AuthOozieClient oozieClient;

    @Autowired
    public OozieClientImpl(AuthOozieClient oozieClient) {
        this.oozieClient = oozieClient;
    }


    @Override
    public String start(Properties properties) throws ApplicationException {
        try {
            Properties _properties = oozieClient.createConfiguration();
            _properties.putAll(properties);;
            return oozieClient.run(_properties);
        } catch (OozieClientException e) {
            e.printStackTrace();;
            throw new ApplicationException("start oozie job failed:" + e.getMessage());
        }
    }

    @Override
    public boolean kill(String jobId) {
        try {
            oozieClient.kill(jobId);
            return true;
        } catch (OozieClientException e) {
            e.printStackTrace();
            return  false;
        }
    }

    @Override
    public String status(String jobId) {
        try {
            return  oozieClient.getStatus(jobId);
        } catch (OozieClientException e) {
            e.printStackTrace();
            return "error";
        }
    }

}
